package xyz.naokeziteng.leetcode;

/**
 * @author hawk
 * @date 2022/6/6
 * @desc
 **/
public class Solution {
    public int findContentChildren(int[] g, int[] s) {
        if (s.length == 0) {
            return 0;
        }
        int children = 0;
        for (int i = 0; i < g.length; i++) {
            int o = children(g[i], s);
            if (o > -1) {
                children += 1;
                s = removeArr(o, s);

            }
        }
        return children;
    }

    public static int children(int i, int[] s) {
        int m = -1, n = -1;
        for (int j = 0; j < s.length; j++) {
            if (s[j] >= i && m == -1) {
                m = s[j];
                n = j;
            } else if (s[j] >= i && m > 0) {
                if (s[j] < m) {
                    m = s[j];
                    n = j;
                }
            }
        }
        return n;
    }

    public static int[] removeArr(int m, int[] s) {
        int[] arr = new int[s.length - 1];
        int n = 0;
        for (int i = 0; i < s.length; i++) {
            if (i != m) {
                arr[n] = s[i];
                n++;
            }
        }
        return arr;
    }


    public ListNode removeElements(ListNode head, int val) {
        if (head == null) {
            return null;
        }

        ListNode res = removeElements(head.next, val);
        if (head.val == val) {
            return res;
        } else {
            head.next = res;
            return head;
        }

    }


    public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        return addTwoNumbersReal(0, l1, l2);
    }

    public static ListNode addTwoNumbersReal(int carry, ListNode l1, ListNode l2) {
        if (l1 == null && l2 == null) {
            return carry == 1 ? new ListNode(1) : null;
        }

        ListNode result = new ListNode();
        int val1 = l1 == null ? 0 : l1.val;
        int val2 = l2 == null ? 0 : l2.val;
        int sum = carry + val1 + val2;
        result.val = sum % 10;
        result.next = addTwoNumbersReal(sum >= 10 ? 1 : 0, l1 == null ? null : l1.next, l2 == null ? null : l2.next);
        return result;
    }

    public static ListNode swapPairs(ListNode head) {
        if (head == null) {
            return null;
        }
        if (head.next == null) {
            return head;
        }
        ListNode nextLoop = head.next.next;
        ListNode next = head.next;
        next.next = head;
        head.next = swapPairs(nextLoop);
        return next;
    }

    public static ListNode reverseList(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode swap = head.next.next;
        ListNode next = head.next;
        head.next = null;
        next.next = head;
        ListNode loop = reverseList(swap);
        if (loop == null) {
            return next;
        }
        ListNode tmp = loop;
        while (tmp.next != null) {
            tmp = tmp.next;
        }
        tmp.next = next;
        return loop;
    }

    public static ListNode reverseList2(ListNode head) {
        ListNode prev = null;
        ListNode cur = head;
        while (cur != null) {
            ListNode next = cur.next;
            cur.next = prev;
            prev = cur;
            cur = next;
        }
        return prev;
    }

    public static ListNode reverseList3(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode newHead = reverseList3(head.next);
        head.next.next = head;
        head.next = null;
        return newHead;
    }

    public static void main(String[] args) {
        ListNode l1 = new ListNode(new int[]{1, 2, 3, 4, 5, 6});
        System.out.println(l1);
        ListNode res = reverseList3(l1);
        System.out.println(res);
    }

}
